package com.fh.db.mysql;

import com.fh.db.*;
import com.fh.db.base.BaseDbTable;
import com.fh.db.oracle.OracleDbColumn;
import com.fh.db.oracle.OracleDbIndex;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class MysqlDbTable extends BaseDbTable {

    @Override
    public DbSource getDbSource() {
        return dbSource;
    }

    public void setDbSource(DbSource dbSource) {
        this.dbSource = dbSource;
    }

    private DbSource dbSource;

    private String tablespaceName;

    public String getTablespaceName() {
        return tablespaceName;
    }

    public void setTablespaceName(String tablespaceName) {
        this.tablespaceName = tablespaceName;
    }

    @Override
    public List<DbColumn> getColumns() {
        return (List<DbColumn>) SqlRunner.preparedSqlResult(dbSource,dbSource.getSqlSet().columnPreSql(),new String[]{this.getOwner(),this.getTableName()},
                resultSet -> {
                    List<DbColumn> result=new ArrayList<>();
                    try{
                        while(resultSet.next()){
                            MysqlDbColumn mysqlDbColumn=new MysqlDbColumn();
                            mysqlDbColumn.setColumnId(resultSet.getBigDecimal("ordinal_position").intValue());
                            mysqlDbColumn.setColumnName(resultSet.getString("column_name"));
                            mysqlDbColumn.setColumnComments(resultSet.getString("column_comment"));
                            mysqlDbColumn.setNullable(resultSet.getString("is_nullable"));
                            mysqlDbColumn.setDataType(resultSet.getString("data_type"));
                            BigDecimal characterMaximumLength=resultSet.getBigDecimal("character_maximum_length");
                            BigDecimal numericPrecision=resultSet.getBigDecimal("numeric_precision");
                            Integer numberPrecisionTmp=numericPrecision==null?null:numericPrecision.intValue();
                            if(characterMaximumLength==null){
                                mysqlDbColumn.setDataLength(numberPrecisionTmp);
                            }else{
                                mysqlDbColumn.setDataLength(characterMaximumLength.intValue());
                            }

                            BigDecimal numericScale=resultSet.getBigDecimal("numeric_scale");
                            mysqlDbColumn.setDataPrecision(numericScale==null?null:numericScale.intValue());
                            result.add(mysqlDbColumn);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    return result;
                });
    }

    @Override
    public List<DbTrigger> getTriggers() {
        return null;
    }

    @Override
    public List<DbIndex> getIndexs() {
        //TODO
        return (List<DbIndex>) SqlRunner.preparedSqlResult(dbSource,dbSource.getSqlSet().indexPreSql(),new String[]{this.getTableName(),this.getOwner()},
                resultSet -> {
                    List<DbIndex> result=new ArrayList<>();
                    try{
                        while(resultSet.next()){
                            OracleDbIndex oracleDbIndex=new OracleDbIndex();
                            oracleDbIndex.setDbSource(dbSource);
                            oracleDbIndex.setIndexName(resultSet.getString("INDEX_NAME"));
                            oracleDbIndex.setIndexType(resultSet.getString("INDEX_TYPE"));
                            oracleDbIndex.setOwner(resultSet.getString("OWNER"));
                            oracleDbIndex.setUniqueness(resultSet.getString("UNIQUENESS"));
                            result.add(oracleDbIndex);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    return result;
                });
    }
}
